<?php
require_once 'car.class.php';
class CarDao {
	function save($entCar) {
		$objDB = $GLOBALS['objDB'];
		$objMDB2 = $GLOBALS['objMDB2'];
		$rscCar = $objDB->Execute("SELECT * FROM car WHERE id=-1 ;");
		$arrRecords = array();
		$arrRecords['make'] = $entCar->make;
		$arrRecords['model'] = $entCar->model;
		$arrRecords['price'] = $entCar->price;
		$arrRecords['color'] = $entCar->color;
		$arrRecords['photo'] = $entCar->photo;
		$arrRecords['pic1'] = $entCar->pic1;
		$arrRecords['pic2'] = $entCar->pic2;
		$arrRecords['pic3'] = $entCar->pic3;
		$arrRecords['pic4'] = $entCar->pic4;
		$arrRecords['vehicle'] = $entCar->vehicle; 
		$arrRecords['door'] = $entCar->door; 
		$arrRecords['cylind'] = $entCar->cylind; 
		$arrRecords['fuel'] = $entCar->fuel; 
		$arrRecords['gear'] = $entCar->gear; 
		$arrRecords['gearbox'] = $entCar->gearbox; 
		$arrRecords['odometer'] = $entCar->odometer; 
		$arrRecords['rego'] = $entCar->rego; 
		$arrRecords['stock'] = $entCar->stock; 
		$arrRecords['utime'] = $entCar->utime; 
		$arrRecords['vin'] = $entCar->vin; 
		$arrRecords['comments'] = $entCar->comments; 

		$strInsertSQL = $objDB->GetInsertSQL($rscCar,$arrRecords);
		if ($objDB->Execute($strInsertSQL)){
			return $objMDB2->lastInsertID("car", "id");			
		} else {
			return 0;
		}
	}
	
	function saveOptions($carId, $options) {
		$mdb2 = $GLOBALS['objMDB2'];
		CarDao::deleteOptions($carId);
		foreach ($options as $optionId){
			$mdb2->queryOne("INSERT INTO car_caroption VALUES($carId,$optionId) ;");
		}
	}
	
	function deleteOptions($carId) {
		$objDB = $GLOBALS['objDB'];
		$objDB->Execute("DELETE FROM car_caroption WHERE cid=$carId;");
	}	
		
	function findAll(){
		$objDB = $GLOBALS['objDB'];
		$rscCar = $objDB->Execute("SELECT * FROM car ORDER BY id DESC;");
		while(!$rscCar->EOF){
			$entCar = new Car($rscCar->fields['id'],$rscCar->fields['make'],$rscCar->fields['model'],$rscCar->fields['price'],$rscCar->fields['color'],$rscCar->fields['photo']);
			$arrCar[] = $entCar;
			$rscCar->MoveNext();
		}		
		return $arrCar;
	}
	
	function findAllPaging($pager) {
		$mdb2 = $GLOBALS['objMDB2'];
		//first, we use Pager to create the links
		$num_cars = $mdb2->queryOne('SELECT COUNT(*) FROM car');
		$pager_options = array(
		    'mode'       => 'Sliding',
		    'perPage'    => 4,
		    'delta'      => 2,
		    'totalItems' => $num_cars,
		);
		
		$pager = Pager::factory($pager_options);
		
		
		//then we fetch the relevant records for the current page
		list($from, $to) = $pager->getOffsetByPageId();
		//set the OFFSET and LIMIT clauses for the following query
		$mdb2->setLimit($pager_options['perPage'], $from - 1);
		$query = 'SELECT id,make, model,price,color,photo FROM car ORDER BY id DESC;';
		$cars = $mdb2->queryAll($query, null, MDB2_FETCHMODE_ASSOC);
		foreach ($cars as $row) {
 	   		$car = new Car($row['id'],$row['make'],$row['model'],$row['price'],$row['color'],$row['photo']);
			$carList[] = $car;
		}
		return $carList;		
	}

	function getTotalNumber() {
		$mdb2 = $GLOBALS['objMDB2'];
		return $mdb2->queryOne('SELECT COUNT(*) FROM car');
	}
		
	function findAllOptions() {
		$mdb2 = $GLOBALS['objMDB2'];
		return $mdb2->queryAll("SELECT id,name FROM caroption ORDER BY name ASC;", null, MDB2_FETCHMODE_ASSOC);	
	}
	
	
	function find($id) {
		$objDB = $GLOBALS['objDB'];
		$rsCar = $objDB->Execute("SELECT * FROM car WHERE id=$id ;");
		if ($rsCar) {
			$id 	= $rsCar->fields['id'];
			$make 	= $rsCar->fields['make'];
			$model	= $rsCar->fields['model'];
			$price	= $rsCar->fields['price'];
			$color	= $rsCar->fields['color'];
			$photo	= $rsCar->fields['photo'];
			$pic1	= $rsCar->fields['pic1'];
			$pic2	= $rsCar->fields['pic2'];
			$pic3	= $rsCar->fields['pic3'];
			$pic4	= $rsCar->fields['pic4'];
			$vehicle = $rsCar->fields['vehicle'];
			$door 	= $rsCar->fields['door'];
			$cylind = $rsCar->fields['cylind'];
			$fuel 	= $rsCar->fields['fuel'];
			$gear 	= $rsCar->fields['gear'];
			$gearbox = $rsCar->fields['gearbox'];
			$odometer = $rsCar->fields['odometer'];
			$rego 	= $rsCar->fields['rego'];
			$stock 	= $rsCar->fields['stock'];
			$utime 	= $rsCar->fields['utime'];
			$vin 	= $rsCar->fields['vin'];
			$comments = $rsCar->fields['comments'];
			$entCar = new Car($id,$make,$model,$price,$color,$photo);
			$entCar->setDetails($vehicle, $door, $cylind, $fuel, $gear, $gearbox, $odometer,$rego,$stock,$utime,$vin,$comments);
			$entCar->setPics($pic1,$pic2,$pic3,$pic4); 
		} else {
			$entCar = new Car(0,"","","","","");
		}
		return $entCar;
	}
	
	function findCarOptions($car){
		$mdb2 = $GLOBALS['objMDB2'];
		$options = $mdb2->queryAll("SELECT oid from car_caroption where cid=$car->id", null , MDB2_FETCHMODE_ASSOC);
		foreach ($options as $option) {
			$car->addToOptions($option['oid']);			
		}
		return $car;
	} 
	
	function update($entCar) {
		$objDB = $GLOBALS['objDB'];
		if ($objDB->Execute("UPDATE car SET 
							make='$entCar->make', model='$entCar->model' , 
							price='$entCar->price', color='$entCar->color', 
							photo='$entCar->photo', pic1='$entCar->pic1',
							pic2='$entCar->pic2', pic3='$entCar->pic3',
							pic4='$entCar->pic4', vehicle='$entCar->vehicle',
							door='$entCar->door', cylind='$entCar->cylind',
							fuel='$entCar->fuel', gear='$entCar->gear',
							gearbox='$entCar->gearbox', odometer='$entCar->odometer',
							rego='$entCar->rego', stock='$entCar->stock',
							utime=NOW(), vin='$entCar->vin',
							comments='$entCar->comments' WHERE id='$entCar->id' ;")){
			return true;
		} else {
			return false;
		}
	}		

	function delete($id) {
		$objDB = $GLOBALS['objDB'];
		CarDao::deleteCarPictures($id);		
		if ($objDB->Execute("DELETE FROM car WHERE id=$id ;")) {
			return true;
		} else {
			return false;
		}
	}

	function deleteCarPictures($id) {
		$mdb2 = $GLOBALS['objMDB2'];
		$result = $mdb2->queryAll("SELECT photo,pic1,pic2,pic3,pic4 FROM car WHERE id=$id;",null, MDB2_FETCHMODE_ASSOC);
		foreach ($result as $row) {
			$photo = $row['photo'];
			if ($photo != "" && !is_null($photo)) {
				$tmpPhoto = $GLOBALS['arrCfg']['strPhysicalPath']."/img/cars/".$photo;
				$thbPhoto = $GLOBALS['arrCfg']['strPhysicalPath']."/img/cars/_thb_".$photo;
				if (file_exists($tmpPhoto)) { unlink ($tmpPhoto);}
				if (file_exists($thbPhoto)) { unlink ($thbPhoto);}
			}
			$pic1 = $row['pic1'];
			if ($pic1 != "" && !is_null($pic1)) {
				$tmpPic1 = $GLOBALS['arrCfg']['strPhysicalPath']."/img/cars/".$pic1;
				$thbPic1 = $GLOBALS['arrCfg']['strPhysicalPath']."/img/cars/_thb_".$pic1;
				if (file_exists($tmpPic1)) { unlink ($tmpPic1);}
				if (file_exists($thbPic1)) { unlink ($thbPic1);}
			}
			$pic2 = $row['pic2'];
			if ($pic2 != "" && !is_null($pic2)) {
				$tmpPic2 = $GLOBALS['arrCfg']['strPhysicalPath']."/img/cars/".$pic2;
				$thbPic2 = $GLOBALS['arrCfg']['strPhysicalPath']."/img/cars/_thb_".$pic2;
				if (file_exists($tmpPic2)) { unlink ($tmpPic2);}
				if (file_exists($thbPic2)) { unlink ($thbPic2);}
			}
			$pic3 = $row['pic3'];
			if ($pic3 != "" && !is_null($pic3)) {
				$tmpPic3 = $GLOBALS['arrCfg']['strPhysicalPath']."/img/cars/".$pic3;
				$thbPic3 = $GLOBALS['arrCfg']['strPhysicalPath']."/img/cars/_thb_".$pic3;
				if (file_exists($tmpPic3)) { unlink ($tmpPic3);}
				if (file_exists($thbPic3)) { unlink ($thbPic3);}
			}
			$pic4 = $row['pic4'];
			if ($pic4 != "" && !is_null($pic4)) {
				$tmpPic4 = $GLOBALS['arrCfg']['strPhysicalPath']."/img/cars/".$pic4;
				$thbPic4 = $GLOBALS['arrCfg']['strPhysicalPath']."/img/cars/_thb_".$pic4;
				if (file_exists($tmpPic4)) { unlink ($tmpPic4);}
				if (file_exists($thbPic4)) { unlink ($thbPic4);}
			}
		}
	
	}
	
}
?>